package com.example.happ.controller;

import com.example.happ.pojo.StoreMerchant;
import com.example.happ.service.MerchantInfoService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
//758e556ad75888318984c311c0a05260
import javax.annotation.Resource;
import java.util.Map;

@RestController  // 返回json数据
public class loginController {
    private final String APPID = "wx28674d4f41a7bd61";
    private final String SECRET = "758e556ad75888318984c311c0a05260";
    private final String WX_API_URL = "https://api.weixin.qq.com/sns/jscode2session";
    @Resource
    private MerchantInfoService merchantInfoService;

    @RequestMapping("/login")//绑定商家
    public StoreMerchant saveMerchantByCode(@RequestBody Map<String,String> codeMap){
        StoreMerchant success=null;
        String code= codeMap.get("code");
        String avatarUrl= codeMap.get("avatarUrl");
        String nickName= codeMap.get("name");
        System.out.println(codeMap.get("nickname"));
        if (code == null || code.isEmpty()) {
            throw new RuntimeException("code不能为空");
        }
        // 拼接请求URL
        String requestUrl=String.format("%s?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",WX_API_URL,APPID,SECRET,code);
        // 发送HTTP请求
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(requestUrl, String.class);
        //解析返回结果
        ObjectMapper objectMapper= new ObjectMapper();
        try {
            Map<String,Object> result=objectMapper.readValue(response,Map.class);
            if (result.containsKey("errcode")) {
                throw new RuntimeException("微信登录失败：" + result.get("errmsg"));
            }
            String openId=(String) result.get("openid");
            success=merchantInfoService.insertMerchantInformationByOpenid(openId,avatarUrl,nickName);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("解析微信接口响应失败", e);
        }
        return success;
    }
    @RequestMapping("/find")//绑定页面第一次请求
    @CrossOrigin(origins = "http://localhost:8080")
    public int findMerchantByCode(@RequestBody Map<String,String> codeMap){
        int success=0;
        String code= codeMap.get("code");
        System.out.println(code);
        if (code == null || code.isEmpty()) {
            throw new RuntimeException("code不能为空");
        }
        String requestUrl=String.format("%s?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",WX_API_URL,APPID,SECRET,code);
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(requestUrl, String.class);
        ObjectMapper objectMapper= new ObjectMapper();
        try {
            Map<String,Object> result=objectMapper.readValue(response,Map.class);
            if (result.containsKey("errcode")) {
                throw new RuntimeException("微信登录失败：" + result.get("errmsg"));
            }
            String openId=(String) result.get("openid");
            success=merchantInfoService.findMerchantInformationByOpenid(openId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("解析微信接口响应失败", e);
        }
        return success;
    }
}
